package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.bo.POrderBo;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PVipOrderBo;
import com.ruoyi.system.domain.vo.PVipOrderVo;
import com.ruoyi.system.domain.PVipOrder;
import com.ruoyi.system.mapper.PVipOrderMapper;
import com.ruoyi.system.service.IPVipOrderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 会员订单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-02
 */
@RequiredArgsConstructor
@Service
public class PVipOrderServiceImpl implements IPVipOrderService {

    private final PVipOrderMapper baseMapper;
    private final SysUserMapper sysUserMapper;
    private final POrderServiceImpl pOrderService;

    /**
     * 查询会员订单
     */
    @Override
    public PVipOrderVo queryById(Long vipOrderId){
        return baseMapper.selectVoById(vipOrderId);
    }

    /**
     * 查询会员订单列表
     */
    @Override
    public TableDataInfo<PVipOrderVo> queryPageList(PVipOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PVipOrder> lqw = buildQueryWrapper(bo);
        Page<PVipOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询会员订单列表
     */
    @Override
    public List<PVipOrderVo> queryList(PVipOrderBo bo) {
        LambdaQueryWrapper<PVipOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PVipOrder> buildQueryWrapper(PVipOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PVipOrder> lqw = Wrappers.lambdaQuery();
        lqw.like(bo.getVipOrderId() != null, PVipOrder::getVipOrderId, bo.getVipOrderId());
        lqw.eq(bo.getPayPrice() != null, PVipOrder::getPayPrice, bo.getPayPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), PVipOrder::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getVipValue()), PVipOrder::getVipValue, bo.getVipValue());
        lqw.eq(bo.getUserId() != null, PVipOrder::getUserId, bo.getUserId());
        return lqw;
    }

    /**
     * 新增会员订单
     */
    @Override
    public Boolean insertByBo(PVipOrderBo bo) {
        PVipOrder add = BeanUtil.toBean(bo, PVipOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setVipOrderId(add.getId());
        }
        return flag;
    }

    /**
     * 修改会员订单
     */
    @Override
    public Boolean updateByBo(PVipOrderBo bo) {
        PVipOrder update = BeanUtil.toBean(bo, PVipOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PVipOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除会员订单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void insertOrderByBo(POrderBo bo) {
        PVipOrder order = new PVipOrder();
        order.setVipOrderId(bo.getOrderNo())
            .setStatus("0")
            .setPayPrice(bo.getPrice())
            .setUserId(LoginHelper.getUserId())
            .setVipValue(bo.getVipValue());
        this.baseMapper.insert(order);
    }

    @Override
    public void askPayOk(String orderId) {
        PVipOrder order = this.baseMapper.selectOne(new LambdaQueryWrapper<PVipOrder>().eq(PVipOrder::getVipOrderId, orderId));
        this.baseMapper.update(null, new LambdaUpdateWrapper<PVipOrder>().set(PVipOrder::getStatus, 1).eq(PVipOrder::getVipOrderId, order.getVipOrderId()));
        this.sysUserMapper.update(null, new LambdaUpdateWrapper<SysUser>().set(SysUser::getMemberId, order.getVipValue()).eq(SysUser::getUserId, order.getUserId()));
        SysUser sysUser = this.sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserId, order.getUserId()));
        this.pOrderService.setTheCommission(sysUser, order.getPayPrice(), Long.parseLong(orderId));
    }
}
